﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PropertyDataContext.UnitOfWork;
using PropertyEntities.ResponseEntities;
using PropertyEntities.RequestEntities;
using PropertyDataContext.Model;

namespace PropertyDAL
{
    public class AgentDAL
    {
        UOWEntities uow = new UOWEntities();
        public AgentResponse RegisterAgent(AgentRequest agentRequest)
        {
            AgentResponse agentReponse = new AgentResponse();

            try
            {
                bool isUsernameExist = uow.agentRepositery.Get().Any(ad => ad.Username == agentRequest.Username);
                bool isEmailExist = uow.agentRepositery.Get().Any(ad => ad.Email == agentRequest.Email);
                if (isEmailExist == false)
                {
                    if (isUsernameExist == false)
                    {
                        Agent agent = new Agent();
                        agent.Address1 = agentRequest.Address1;
                        agent.Address2 = agentRequest.Address2;
                        agent.City = agentRequest.City;
                        agent.Country = agentRequest.Country;
                        agent.Email = agentRequest.Email;
                        agent.IdNo = agentRequest.IdNo;
                        agent.IDType = agentRequest.IDType;
                        agent.MobileNo = agentRequest.MobileNo;
                        agent.Name = agentRequest.Name;
                        agent.Nationality = agentRequest.Nationality;
                        agent.Password = agentRequest.Password;
                        agent.PhoneNo = agentRequest.PhoneNo;
                        agent.Username = agentRequest.Username;
                        uow.agentRepositery.Insert(agent);
                        uow.Save();
                        agentReponse.SuccessMessage = "Agent Registered Successfully";
                        agentReponse.IsResult = true;
                    }
                    else
                    {
                        agentReponse.ErrorMessage = "Username already exist.";
                        agentReponse.IsResult = false;
                    }
                }
                else
                {
                    agentReponse.ErrorMessage = "Email address already exist.";
                    agentReponse.IsResult = false;
                }
            }
            catch
            {
                agentReponse.ErrorMessage = "Some error occured.";
                agentReponse.IsResult = false;
            }

            return agentReponse;
        }

        public AgentResponse DeleteAgent(string AgentId)
        {
            AgentResponse agentResponse = new AgentResponse();

            try
            {
                Agent agent = uow.agentRepositery.Get().Where(ad => ad.AgentID == Convert.ToInt32(AgentId)).FirstOrDefault();
                if (agent != null)
                {
                    uow.agentRepositery.Delete(AgentId);
                    agentResponse.SuccessMessage = "Agent Deleted Successfully";
                    agentResponse.IsResult = true;
                }
                else
                {
                    agentResponse.ErrorMessage = "Agent doesn't exist.";
                    agentResponse.IsResult = false;
                }
            }
            catch
            {
                agentResponse.ErrorMessage = "Some error occured.";
                agentResponse.IsResult = false;
            }
            return agentResponse;
        }

        public AgentResponse UpdateAgent(AgentRequest agentRequest)
        {
            AgentResponse agentReponse = new AgentResponse();

            try
            {
                bool isUsernameExist = uow.agentRepositery.Get().Any(ad => ad.Username == agentRequest.Username);
                bool isEmailExist = uow.agentRepositery.Get().Any(ad => ad.Email == agentRequest.Email);
                if (isEmailExist == false)
                {
                    if (isUsernameExist == false)
                    {
                        Agent agent = uow.agentRepositery.Get().Where(ad => ad.AgentID == agentRequest.AgentID).FirstOrDefault();
                        agent.Address1 = agentRequest.Address1;
                        agent.Address2 = agentRequest.Address2;
                        agent.City = agentRequest.City;
                        agent.Country = agentRequest.Country;
                        agent.Email = agentRequest.Email;
                        agent.IdNo = agentRequest.IdNo;
                        agent.IDType = agentRequest.IDType;
                        agent.MobileNo = agentRequest.MobileNo;
                        agent.Name = agentRequest.Name;
                        agent.Nationality = agentRequest.Nationality;
                        agent.Password = agentRequest.Password;
                        agent.PhoneNo = agentRequest.PhoneNo;
                        agent.Username = agentRequest.Username;
                        uow.agentRepositery.Insert(agent);
                        uow.Save();
                        agentReponse.SuccessMessage = "Agent Updated Successfully";
                        agentReponse.IsResult = true;
                    }
                    else
                    {
                        agentReponse.ErrorMessage = "Username already exist.";
                        agentReponse.IsResult = false;
                    }
                }
                else
                {
                    agentReponse.ErrorMessage = "Email address already exist.";
                    agentReponse.IsResult = false;
                }
            }
            catch
            {
                agentReponse.ErrorMessage = "Some error occured.";
                agentReponse.IsResult = false;
            }

            return agentReponse;
        }
    }
}
